package org.dreamfly.core.dao.impl;

import org.dreamfly.core.bean.SzPicture;
import org.dreamfly.core.dao.PictureDAO;
import org.dreamfly.core.util.HTMLInputFilter;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.List;

import static org.hibernate.criterion.Restrictions.like;
import static org.hibernate.criterion.Restrictions.or;

/**
 * Created by IntelliJ IDEA. User: Gin Date: 2010-9-15 Time: 22:06:08 To change
 * this template use File | Settings | File Templates.
 */
/**
 * @author Administrator 
 * yao 已做修改
 */
@SuppressWarnings("deprecation")
public class PictureDAOImpl extends BaseDAOImpl<SzPicture, Long> implements
		PictureDAO {
	@SuppressWarnings( { "unchecked" })
	@Override
	public List<SzPicture> findByType(List<Long> pictureTypes, String title,
			int firstRow, int maxRow) {
		/**
		 * 判断空值
		 */
		if (pictureTypes == null || pictureTypes.size() == 0)
			return null;
		/**
		 * 将空格全部去掉
		 */
		if (title == null || title.length() == 0)
			return null;
		title = title.replace(" ", "");
		if (title.isEmpty())
			return null;
		/**
		 * 开始查询操作
		 */
		Criteria criteria = this.getSession().createCriteria(SzPicture.class);
		criteria.add(Expression.in("szPictureType.typeId", pictureTypes));
		
		/**
		 * 不为空的状况下 执行查询操作
		 */
		if (title != null && title.length() > 0)
			criteria.add(or(like("title", "%" + title + "%"), like("note", "%" + title + "%")));
		/**
		 * 按标题来排序
		 */
		criteria.addOrder(Order.asc("title"));
		criteria.setFirstResult(firstRow);
		criteria.setMaxResults(maxRow);
		List list = criteria.list();

		return ((null == list) || list.isEmpty()) ? null : list;
	}
	
	@Override
	public List<SzPicture> findNativeByType(List<Long> pictureTypes,
			String title, String serverName, int firstRow, int maxRow) {
		/**
		 * 判断空值
		 */
		if (pictureTypes == null || pictureTypes.size() == 0)
			return null;
		/**
		 * 将空格全部去掉
		 */
		if (title == null || title.length() == 0)
			return null;
		title = title.replace(" ", "");
		if (title.isEmpty())
			return null;
		/**
		 * 开始查询操作
		 */
		Criteria criteria = this.getSession().createCriteria(SzPicture.class);
		criteria.add(Expression.in("szPictureType.typeId", pictureTypes));
		criteria.add(Restrictions.eq("server", serverName));
		/**
		 * 不为空的状况下 执行查询操作
		 */
		if (title != null && title.length() > 0)
			criteria.add(or(like("title", "%" + title + "%"),like("note", "%" + title + "%")));
		/**
		 * 按标题来排序
		 */
		criteria.addOrder(Order.asc("title"));
		criteria.setFirstResult(firstRow);
		criteria.setMaxResults(maxRow);
		List list = criteria.list();

		return ((null == list) || list.isEmpty()) ? null : list;
	}



	@SuppressWarnings( { "unchecked" })
	@Override
	public Long findCount(List<Long> pictureTypes, String title) {

		if (pictureTypes == null || pictureTypes.size() == 0)
			return 0L;

		StringBuffer bs = new StringBuffer();

		/*
		 * for (int i = 0; i < pictureTypes.size(); i++) bs.append("," +
		 * pictureTypes.get(i));
		 */
		for (Long l : pictureTypes) {
			bs.append(",");
			bs.append(l);
		}
		/*
		 * String sql = "select count(*) from " + SzPicture.class.getName() +
		 * " where pictureTypeId in(" + bs.toString().substring(1) + ")";
		 */
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from ");
		sql.append(" ");
		sql.append(SzPicture.class.getName());
		sql.append(" ");
		sql.append("where typeId in(");
		sql.append(bs.toString().substring(1));
		sql.append(")");

		/**
		 * 对tilte进行判断
		 */
		if (title == null || title.isEmpty())
			return 0L;
		title = title.replace(" ", "");
		if (title.isEmpty())
			return 0L;
		// if (title != null && title.length() > 0)
		sql.append(" and (title like '%" + title + "%'");
        sql.append(" or note like");
            sql.append("'%");
			sql.append(title);
			sql.append("%')");
		// sql += " and title like '%" + title + "%'";
		List list = this.getSession().createQuery(sql.toString()).list();
	
		/**
		 * isEmpty开销是固定的值   
		 */
		return (null == list||list.isEmpty()) ? 0L : (Long)list.get(0);
	}

	
	
	@Override
	public Long findNativeCount(List<Long> pictureTypes, String title, String serverName) {
		if (pictureTypes == null || pictureTypes.size() == 0)
			return 0L;

		StringBuffer bs = new StringBuffer();

		/*
		 * for (int i = 0; i < pictureTypes.size(); i++) bs.append("," +
		 * pictureTypes.get(i));
		 */
		for (Long l : pictureTypes) {
			bs.append(",");
			bs.append(l);
		}
		/*
		 * String sql = "select count(*) from " + SzPicture.class.getName() +
		 * " where pictureTypeId in(" + bs.toString().substring(1) + ")";
		 */
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from ");
		sql.append(" ");
		sql.append(SzPicture.class.getName());
		sql.append(" ");
		sql.append("where typeId in(");
		sql.append(bs.toString().substring(1));
		sql.append(")");

		/**
		 * 对tilte进行判断
		 */
		if (title == null || title.isEmpty())
			return 0L;
		title = title.replace(" ", "");
		if (title.isEmpty())
			return 0L;
		// if (title != null && title.length() > 0)
		sql.append(" and (title like '%" + title + "%'");
        sql.append(" or note like");
            sql.append("'%");
			sql.append(title);
			sql.append("%')");
		sql.append(" ");
		sql.append("and ");
		sql.append("server = '");
		sql.append(serverName);
		sql.append("'");
		// sql += " and title like '%" + title + "%'";
		List list = this.getSession().createQuery(sql.toString()).list();
	
		/**
		 * isEmpty开销是固定的值   
		 */
		return (null == list||list.isEmpty()) ? 0L : (Long)list.get(0);
	}

	@SuppressWarnings( { "unchecked" })
	@Override
	public SzPicture findByUrl(String pictureUrls
    ) {
		Criteria criteria = this.getSession().createCriteria(SzPicture.class);
		criteria.add(Expression.eq("pictureUrl", pictureUrls));
		/**
		 *分页操作
		 */
		List list = criteria.list();
		return (null != list && 0 != list.size()) ?(SzPicture)list.get(0) :null ;
	}


	@Override
	public List<SzPicture> normalSearch(SzPicture szPicture, int pageSize,
			int Page) {
	/*	Criteria criteria = this.getSession().createCriteria(SzPicture.class);
    	criteria.add(Example.create(szPicture).enableLike(MatchMode.ANYWHERE));
		List<SzPicture> list = criteria.list();
		return ((null == list) || list.isEmpty()) ? null : list;*/
		return null;
		/**
		 * 此方法现不用
		 */
	}

	@Override
	public void saveInfo(SzPicture picture) {
		HTMLInputFilter inputFilter = new HTMLInputFilter();
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		
		SzPicture szPicture = (SzPicture) session.load(SzPicture.class, picture.getId());
		szPicture.setTitle(inputFilter.filter(picture.getTitle()));
        szPicture.setServer(inputFilter.filter(picture.getServer()));
        szPicture.setNote(inputFilter.filter(picture.getNote()));
		
        tx.commit();
		session.flush();
	}

    @Override
    public void modifyInfo(SzPicture picture) {
        HTMLInputFilter inputFilter = new HTMLInputFilter();
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();

		SzPicture szPicture = (SzPicture) session.load(SzPicture.class, picture.getId());
		szPicture.setTitle(inputFilter.filter(picture.getTitle()));
        szPicture.setNote(inputFilter.filter(picture.getNote()));

        tx.commit();
		session.flush();
    }

}
